<?php
namespace Common\Model;

use Think\Model;
use Org\Nx\Page;

class UserModel extends Model {
    
    public static function newSelf()
    {
        return new self();
    }
    
	public function findUserByUserId($user_id)
	{
		$where['id']=$user_id;
		return $this->where($where)->find();
	}

	public function findUserByUsername($username)
	{
	    $where['username']=$username;
	    return $this->where($where)->find();
	}

	public function countByPhone($phone)
	{
		$where['phone']=$phone;
		return $this->where($where)->count();
	}
    public function incUserYeji($user_id,$quyu,$num,$type='')
    {
        $this->where(['id'=>$user_id])->setInc($type.'yeji'.$quyu,$num);
    }
    public function decUserYeji($user_id,$quyu,$num,$type='')
    {
        $this->where(['id'=>$user_id])->setDec($type.'yeji'.$quyu,$num);
    }
    
	public function checkUserPassword($password,$password_hash)
	{
		if ($this->passwordEncrypt($password)==$password_hash) {
		     return true;
		}
		return false;
	}
	public function checkUserTwoPassword($two_password,$two_password_hash)
	{
		if ($this->twoPasswordEncrypt($two_password)===$two_password_hash) {
		     return true;
		}
		return false;
	}
	public function passwordEncrypt($password)
	{
		return md5(md5($password).'sgsgxcgsdgsdg');
	}
	public function twoPasswordEncrypt($two_password)
	{
		return md5(md5($two_password).'etwetxcvasdf');
	}
	public function setUserPassword($password,$user_id)
	{
		$where['id']=$user_id;
		return $this->where($where)->setField('password',$this->passwordEncrypt($password));
	}
	public function setUserTwoPassword($password,$user_id)
	{
		$where['id']=$user_id;
		return $this->where($where)->setField('twopassword',$this->twoPasswordEncrypt($password));
	}
    public function decUserMondey($user_id,$money,$money_id)
    {
    	$my_money=$this->findUserByUserId($user_id);
    	if ($my_money['money'.$money_id]<$money)
    	{
    		return false;
    	}
    	if ($this->where(['id'=>$user_id])->setDec('money'.$money_id,$money))
    	{
    		return true;
    	}
    	else
    	{
    		return false;
    	}
    }
    public function incUserMondey($user_id,$money,$money_id)
    {
    	$my_money=$this->findUserByUserId($user_id);
    	if ($this->where(['id'=>$user_id])->setInc('money'.$money_id,$money))
    	{
    		return true;
    	}
    	else
    	{
    		return false;
    	}
    }
    public function findUpUser($up_id,$num=9999999)
    {
        $a=0;
        while($a<$num){
            $user[$a]=$this->findUserByUserId($up_id);
            if (!$user[$a])
            {
                unset($user[$a]);
                return $user;
            }
            $up_id=$user[$a]['jie_id'];
            $a++;
        }
        return $user;
    }
	public function getDownUserByUpAndQuyu($up_id,$quyu)
	{
	    $where['jie_id']=$up_id;
	    $where['quyu']=$quyu;
	    return $this->where($where)->find();
	}
	public function getPedigreeChartData($user_id,$num=5)
	{
	    $arr[1][1]=$this->findUserByUserId($user_id);
	    
	    $k=2;
	    while ($num>=$k)
	    {
	        $n=pow(2,$k-2);
	        $arr[$k]=[];
	        for($i=0;$i<$n;$i++)
	        {
                    $this->getDownUser($arr[$k-1][$i+1]['id'],1+$i*2,$arr[$k]);
	        }
	        $k++;
	    }
	    return $arr;
	}
	public function getDownUser($user_id,$num,&$arr)
	{
	    if (!$user_id) {
	        $arr[$num]=[];
	        $arr[$num+1]==[];
	    }
	    $arr[$num]=$this->getDownUserByUpAndQuyu($user_id,1);
	    $arr[$num+1]=$this->getDownUserByUpAndQuyu($user_id,2);
	    return $arr;
	}
	public function getTuiUserData($user_id)
	{
	    $user=$this->field(['username','id','leavel','status','honor_leavel','realname'])->where(array('tui_id'=>$user_id))->select();
	    foreach ($user as $k=>$v)
	    {
	        $user[$k]['tuinum']=$this->findUserTuiNum($v['id']);
	    }
	    return $user;
	}
	public function findUserTuiNum($user_id)
	{
	    return $this->where(['tui_id'=>$user_id])->count();
	}
	public function findUserByJieIdAndQuyu($jie_id,$quyu)
	{
	    return $this->where(['jie_id'=>$jie_id,'quyu'=>$quyu])->find();
	}

	public function pagingList($where=array(),$map=array(),$num=10,$order='id desc'){
	    $count      = $this->where($map)->where($where)->count();
	    $Page       = new Page($count,$num);
	    foreach($map as $key=>$val) {
	        $Page->parameter[$key]   =   urlencode($val);
	    }
	    $show = $Page->show();
	    $list = $this->where($map)->where($where)->order($order)->limit($Page->firstRow.','.$Page->listRows)->select();
	    foreach ($list as $k=>$v)
	    {
	        $list[$k]['tuiuser']=$this->findUserByUserId($v['tui_id']);
	        $list[$k]['jieuser']=$this->findUserByUserId($v['jie_id']);
	    }
	    return array('page'=>$show,'content'=>$list);
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}